package hk.m4s.lr.repair.test.service;//package com.uelive.app.service.account;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.cache.CacheData;
import framework.common.cache.CacheManager;
import framework.common.cache.CacheUtils;
import framework.common.rsa.Base64Utils;
import framework.common.rsa.RSAUtils;
import framework.common.widgets.dialogs.UeDialog;
import framework.common.zanetwork.AcountUeHttpUrl;
import framework.common.zanetwork.UeHttpUrl;
import framework.common.zanetwork.core.HttpCallback;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.model.AccountModel;
import hk.m4s.lr.repair.test.model.GoodsSignModel;
import hk.m4s.lr.repair.test.model.LatModelResult;
import hk.m4s.lr.repair.test.model.ShopModel;
import hk.m4s.lr.repair.test.model.TwoCodeModel;
import hk.m4s.lr.repair.test.model.VcodeModel;
import hk.m4s.lr.repair.test.utils.LogUtil;
import okhttp3.Response;

/**
 * Created by wq on 2017/3/31.
 * 公共接口
 */

public class AccountSerive extends BaseService{

    public static void login(final Context context,String loginName,String password,final ResponseCallback<AccountModel> callback) {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", "");
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("userName",loginName);
            jsondata.put("passWord", password);
            jsondata.put("type", "4");

            try{
                InputStream inPublic = context.getResources().getAssets().open("rsa_public_key.pem");
                PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
                RSAUtils.printPublicKeyInfo(publicKey);
                // 加密
                byte[] encryptByte = RSAUtils.encryptData(jsondata.toString().getBytes(), publicKey);
                // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
                String afterencrypt = Base64Utils.encode(encryptByte);
                parmas.put("jsonText", afterencrypt);
            }catch(Exception e){
                e.printStackTrace();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpHelper.post(AcountUeHttpUrl.login(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","登录中...",false);
            }

            @Override
            public void onSuccess(Response response, AccountModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }

    public static void regist(final Context context,Map<String, Object> parmas,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.regist(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","注册中...",false);
            }

            @Override
            public void onSuccess(Response response, AccountModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void forgetPass(final Context context,Map<String, Object> parmas,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.retrievePassword(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","修改中...",false);
            }

            @Override
            public void onSuccess(Response response, AccountModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void updatePass(final Context context,Map<String, Object> parmas,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.updatePass(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","修改中...",false);
            }

            @Override
            public void onSuccess(Response response, AccountModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void sendSms(final Context context,Map<String, Object>parmas ,final ResponseCallback<VcodeModel> callback) {

        httpHelper.post(AcountUeHttpUrl.snedSms(), parmas, new HttpCallback<VcodeModel>() {
            @Override
            public void onRequestBefore() {
               // UeDialog.showprogressDialog(context,"","认证中...",false);
            }

            @Override
            public void onSuccess(Response response, VcodeModel o) {
              //  UeDialog.hideProgress();
                LogUtil.d("sendsms","sms",response.toString());
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
              //  UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
               // UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void putPushId(final Context context,Map<String, Object>parmas ,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.putPushId(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {
                // UeDialog.showprogressDialog(context,"","认证中...",false);
            }

            @Override
            public void onSuccess(Response response, AccountModel o) {
                //  UeDialog.hideProgress();
                LogUtil.d("sendsms","sms",response.toString());
                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                //  UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                // UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void columnList (final Context context,Map<String, Object> parmas,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.columnList(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, AccountModel o) {

                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {

                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void provincesAll(final Context context,Map<String, Object> parmas,final ResponseCallback<String> callback) {

        canhttpHelper.post(AcountUeHttpUrl.provincesAll(), parmas, new HttpCallback<String>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, String o) {

                    callback.onSuccess(o);

                   CacheManager.getInstance().addCache(new CacheData(CacheUtils.KEY_TEST,o));
                    SharedPreferencesUtils.addgetSharedPreferences("proint","1");

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void agency(final Context context,Map<String, Object> parmas,final ResponseCallback<LatModelResult> callback) {

        httpHelper.post(UeHttpUrl.agency(), parmas, new HttpCallback<LatModelResult>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, LatModelResult o) {

                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void pushFabu(final Context context,Map<String, Object> parmas,final ResponseCallback<LatModelResult> callback) {

        httpHelper.post(AcountUeHttpUrl.pushFabu(), parmas, new HttpCallback<LatModelResult>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, LatModelResult o) {

                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void deviceByTwoCode(final Context context,Map<String, Object> parmas,final ResponseCallback<TwoCodeModel> callback) {

        httpHelper.post(UeHttpUrl.deviceByTwoCode(), parmas, new HttpCallback<TwoCodeModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, TwoCodeModel o) {

                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void oneKeys(final Context context,Map<String, Object> parmas,final ResponseCallback<TwoCodeModel> callback) {

        httpHelper.post(UeHttpUrl.oneKeys(), parmas, new HttpCallback<TwoCodeModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","加载中...",false);
            }

            @Override
            public void onSuccess(Response response, TwoCodeModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void repairSubmit (final Context context,Map<String, Object> parmas,final ResponseCallback<TwoCodeModel> callback) {

        httpHelper.post(UeHttpUrl.repairSubmit(), parmas, new HttpCallback<TwoCodeModel>() {
            @Override
            public void onRequestBefore() {
                UeDialog.showprogressDialog(context,"","加载中...",false);
            }

            @Override
            public void onSuccess(Response response, TwoCodeModel o) {
                UeDialog.hideProgress();
                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                UeDialog.hideProgress();
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                UeDialog.hideProgress();
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void toolUse(final Context context,Map<String, Object> parmas,final ResponseCallback<TwoCodeModel> callback) {

        httpHelper.post(UeHttpUrl.toolUse(), parmas, new HttpCallback<TwoCodeModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, TwoCodeModel o) {

                callback.onSuccess(o);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {
                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void setShare(final Context context,Map<String, Object> parmas,final ResponseCallback<AccountModel> callback) {

        httpHelper.post(AcountUeHttpUrl.setShare(), parmas, new HttpCallback<AccountModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, AccountModel o) {

                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {

                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void index (final Context context,Map<String, Object> parmas,final ResponseCallback<ShopModel> callback) {

        httpHelper.post(UeHttpUrl.index(), parmas, new HttpCallback<ShopModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onSuccess(Response response, ShopModel o) {

                callback.onSuccess(o);
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

                callback.onError(response, errorCode, null, e);

            }

            @Override
            public void onFailure(Response response, String failureMsg, int code) {

                callback.onError(response, code, failureMsg, null);
            }
        });
    }
    public static void findGoodsByProperty (final Context context,Map<String, Object> parmas,final ResponseCallback<GoodsSignModel> callback) {

//        httpHelper.post(UeHttpUrl.findGoodsByProperty(), parmas, new HttpCallback<GoodsSignModel>() {
//            @Override
//            public void onRequestBefore() {
//
//            }
//
//            @Override
//            public void onSuccess(Response response, GoodsSignModel o) {
//
//                callback.onSuccess(o);
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, Exception e) {
//
//                callback.onError(response, errorCode, null, e);
//
//            }
//
//            @Override
//            public void onFailure(Response response, String failureMsg, int code) {
//
//                callback.onError(response, code, failureMsg, null);
//            }
//        });
    }
}
